package com.sinoglobal.ningxia.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Created by XINDZ on 2017/6/14.
 * 功能: 关于Url的工具类
 */

public class UrlUtils {
    /***
     * 获取url 指定name的value;
     * @param url
     * @param name
     * @return 出现异常返回""
     */
    public static String getValueByName(String url, String name) {
        try {
            String result = "";
            int index = url.indexOf("?");
            String temp = url.substring(index + 1);
            String[] keyValue = temp.split("&");
            for (String str : keyValue) {
                if (str.contains(name)) {
                    result = str.replace(name + "=", "");
                    break;
                }
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getUrlDecoder(String url) {
        try {
            String decode = URLDecoder.decode(url, "utf-8");
            return decode;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }
}
